package com.example.demo.leetcode;

import java.util.*;

public class Solution1 {
    public static void main(String[] args) {
//        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
//        List<List<String>> ss = new Solution().groupAnagrams(strs);
//        System.out.println(ss);
        System.out.println(new Solution2().longestConsecutive(new int[]{0,0}));
    }
}

/**
 * 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
 * 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
 */

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

/**
 * 输入：nums = [100,4,200,1,3,2]
 * 输出：4
 * 解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
 */
class Solution2 {
    public int longestConsecutive(int[] nums) {
        List<Integer> list = new ArrayList<>();
        Set<Integer> list2 = new HashSet<>();
        for (int num : nums) {
            list.add(num);
        }
        Collections.sort(list);
        if(list.size()==1){
            return 1;
        }
        for (int i = 0; i < list.size() - 1; i++) {
            if (list.get(i) + 1 == list.get(i + 1)) {
                list2.add(list.get(i));
                list2.add(list.get(i + 1));
            } else {
//                list2.add(1);
                continue;
            }
        }
        return list2.size()>1?list2.size():1;
    }


}
